import React, {Component} from 'react'
import T from 'prop-types'
import {Timeline} from 'antd'

import './timelineItem.less'

import Utils from '../wea-utils'

import DotCom from './DotCom/index'
import ComVar from './ComVar'

import { WeaLocaleProvider } from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

const {Offset, Week, PrefixCls, Format, Type} = ComVar
const P = PrefixCls + '-item'

class WeaTimelineItem extends Component {
    constructor(props) {
        super(props)
    }

    getActualDate = (dateStr) => {
        let dateArr = dateStr.split('-'),
            date = new Date(dateArr[0], --dateArr[1], dateArr[2])
        return date
    }

    getDateDif = (start, end) => {
        const date = (start - end) / 86400000
        if (date > 0) {
            return Math.abs(Math.floor(date))
        }
        return Math.abs(Math.ceil(date))
    }

    getDateText = (date) => {
        const now = new Date(),
            days = this.getDateDif(now, date),
            yearNow = now.getFullYear(),
            yearDate = date.getFullYear()

        let text = '', format = ''

        if (yearNow >= yearDate) {
            if (days == 0) {
                text += getLabel(15537,"今天")
                format = Format.current
            } else if (days <= Offset) {
                //text += Week[date.getDay()]
                //text += eval("Week.day_"+date.getDay()+"()");
                let _day = date.getDay();
                if(_day==0){
                    text += Week.day_0();
                }else if(_day==1){
                    text += Week.day_1();
                }else if(_day==2){
                    text += Week.day_2();
                }else if(_day==3){
                    text += Week.day_3();
                }else if(_day==4){
                    text += Week.day_4();
                }else if(_day==5){
                    text += Week.day_5();
                }else if(_day==6){
                    text += Week.day_6();
                }
                
                format = Format.week
            } else {
                text += (date.getMonth() + 1) + getLabel(383373,"月") + date.getDate() + getLabel(383374,"日")
                format = Format.month
                // if (yearNow !== yearDate) {
                //     text = (yearDate + '年') + text
                //     format = Format.year
                // }
            }
        } else {
            text = '非法日期'
            format = Format.invalid
        }

        return [format, text]
    }




    render() {
        const {date, type, tipDate,showDot, children} = this.props
        let format = Format.current, text = getLabel(15537,"今天"), tip = Type.current.tipFunc()
        if (!!date) {
            const dateActual = this.getActualDate(date)
            format = this.getDateText(dateActual)
            text = this.getDateText(dateActual)[1]
            tip = Type[type].tipFunc() + ((type == Type.normal.name || type == Type.expired.name) ? (' ' + Utils.DateFormat(tipDate, "yyyy年MM月dd日 HH:mm")) : "")
        }

        let itemDot = DotCom(type, text, tip, format);
        if(showDot==''){
            itemDot = () => {
                return <div style={{"width":"1px","height":"5px","background-color":"#eaeaea","margin-left":"10px"}}/>;
            }
        }

        return (
            <div>
                <Timeline.Item ecId={`${this && this.props && this.props.ecId || ''}_undefined@aruyyr`} className={P}  dot={itemDot}>
                    {children}
                </Timeline.Item>
            </div>
        )
    }
}

WeaTimelineItem.propTypes = {
    type: T.string.isRequired,
    date: T.string.isRequired,//时间轴上日期，格式："yyyy-MM-dd"
    tipDate: T.string.isRequired//提示的具体时间，格式："yyyy-MM-dd hh:mm:ss"
}

WeaTimelineItem.defaultProps = {
    type: Type.normal.name
}

export default WeaTimelineItem